/*
 *  libKara - Library for basic instant messaging functions
 *  Copyright (C) 2007 Markus Karnik
 *
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 3 of the License, 
 *  or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along 
 *  with this program; if not, see <http://www.gnu.org/licenses/>.
 *  
 */

package de.karnik.libkara.proto.xmpp;

import java.util.Vector;

import de.karnik.libkara.pluginspecs.ProtocolSettings;

/**
 * The XMPP settings class to store the xmpp settings.
 *
 * 
 * @author Markus Karnik - markus.karnik@my-design.org
 * @version 1.0
 * @since 1.0
 */
public class XmppSettings extends ProtocolSettings {
	
	/**
	 * The predefined resourceID value.
	 */
	public static String STRING_VALUE_RESOURCEID = "resourceID";
	/**
	 * The predefined connection id value.
	 */
	public static String STRING_VALUE_CONNECTIONID = "connectionID";
	/**
	 * The predefined service id value.
	 */
	public static String STRING_VALUE_SERVICEID = "serviceID";
	/**
	 * The predefined service language value.
	 */
	public static String STRING_VALUE_SERVICELANG = "serviceLang";
	/**
	 * The predefined session id value.
	 */
	public static String STRING_VALUE_SESSIONID = "sessionID";
	
	/**
	 * The predefined client tls enabled value.
	 */
	public static String BOOLEAN_VALUE_CLIENT_TLS_ENABLED = "client_tls_enabled";
	/**
	 * The predefined tls enabled value.
	 */
	public static String BOOLEAN_VALUE_TLS_ENABLED = "tls_enabled";
	/**
	 * The predefined tls required value.
	 */
	public static String BOOLEAN_VALUE_TLS_REQUIRED = "tls_required";	
	/**
	 * The predefined tls established value.
	 */
	public static String BOOLEAN_VALUE_TLS_ESTABLISHED = "tls_established";
	/**
	 * The predefined bind required value.
	 */
	public static String BOOLEAN_VALUE_BIND_REQUIRED = "bind_required";
	/**
	 * The predefined bound value.
	 */
	public static String BOOLEAN_VALUE_BOUND = "bound";
	/**
	 * The predefined session required value.
	 */
	public static String BOOLEAN_VALUE_SESSION_REQUIRED = "session_required";
	/**
	 * The predefined authenticated value.
	 */
	public static String BOOLEAN_VALUE_AUTHENTICATED = "authenticated";
	/**
	 * The predefined session established value.
	 */
	public static String BOOLEAN_VALUE_SESSION_ESTABLISHED = "session_established";
	
	/**
	 * Storage to hold the server enabled compression methods.
	 */
	private Vector<String> enabledCompressionMethods = new Vector<String>();
	/**
	 * Storage to hold the server enabled sasl auth methods.
	 */
	private Vector<String> enabledAuthMethods = new Vector<String>();
	
	/**
	 * Constructs a new XMPP settings object with default values.
	 */
	public XmppSettings() {
		super();
		
		this.addRequiredStringValue( STRING_VALUE_RESOURCEID );
		this.addRequiredStringValue( STRING_VALUE_SERVICEID );
		
		this.addRequiredBooleanValue( BOOLEAN_VALUE_CLIENT_TLS_ENABLED );
		
		this.putIntegerData( ProtocolSettings.INTEGER_VALUE_PORT, 5222 );
	}
	
	/**
	 * Constructs a new XMPP settings object with the given values.
	 * 
	 * @param protocolSettings The predefined protocol settings to use.
	 */
	public XmppSettings( ProtocolSettings protocolSettings ) {
		this();
		
		this.putStringData( XmppSettings.STRING_VALUE_HOSTNAME, 
				protocolSettings.getStringData( ProtocolSettings.STRING_VALUE_HOSTNAME ) );

		this.putStringData( XmppSettings.STRING_VALUE_PASSWORD, 
				protocolSettings.getStringData( ProtocolSettings.STRING_VALUE_PASSWORD ) );
		
		this.putIntegerData( XmppSettings.INTEGER_VALUE_PORT, 
				protocolSettings.getIntegerData( ProtocolSettings.INTEGER_VALUE_PORT ) );
	}

	/* (non-Javadoc)
	 * @see de.karnik.libkara.pluginspecs.ProtocolSettings#getCompleteUserID()
	 */
	public String getCompleteUserID() {
		
		StringBuffer sb = new StringBuffer();
		
		sb.append( this.getStringData( STRING_VALUE_USERID) )
					.append( '@' ).append( this.getStringData( STRING_VALUE_SERVICEID ) );
		
		if( this.getStringData( STRING_VALUE_RESOURCEID ) != null )
			sb.append( '/' ).append( this.getStringData( STRING_VALUE_RESOURCEID ) );
		
		return sb.toString();
	}

	/**
	 * Returns the enabled compression methods.
	 * 
	 * @return The enabled compression methods for the connection.
	 */
	public Vector<String> getEnabledCompressionMethods() {
		return enabledCompressionMethods;
	}

	/**
	 * Adds a enabled compression method.
	 * 
	 * @param compressionMethod The enabled compression method to add.
	 */
	public void addEnabledCompressionMethods( String compressionMethod ) {
		if( !this.enabledCompressionMethods.contains( compressionMethod ) )
			this.enabledCompressionMethods.add( compressionMethod );
	}

	/**
	 * Returns the enabled authentication methods.
	 * 
	 * @return The enabled authentication methods for this connection.
	 */
	public Vector<String> getEnabledAuthMethods() {
		return enabledAuthMethods;
	}

	/**
	 * Adds a enabled authentication method.
	 * 
	 * @param authMethod The enabled authentication method to add.
	 */
	public void addEnabledAuthMethods( String authMethod ) {
		if( !this.enabledAuthMethods.contains( authMethod ) )
			this.enabledAuthMethods.add( authMethod );
	}

}
